package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecGroupController {

    @Autowired
    private SpecGroupService specGroupService;

    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroupByCid(@PathVariable("cid")Long cid){
        List<SpecGroup> groups = specGroupService.querySpecGroupByCid(cid);
        if(groups == null || groups.size() == 0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(groups);
    }

    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> querySpecParamByGid(
            @RequestParam(value="gid" , required = false)Long gid,
            @RequestParam(value="cid" , required = false)Long cid,
            @RequestParam(value = "searching",required = false)Boolean searching,
            @RequestParam(value = "generic",required = false) Boolean generic
    ){

        List<SpecParam> params = specGroupService.querySpecParamBuGid(gid,cid,searching,generic);
        if(params == null || params.size() == 0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(params);
    }

    @PostMapping("group")
    public ResponseEntity<String> addSpecGroup(SpecGroup group){

        this.specGroupService.addSpecGroup(group);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping("group")
    public ResponseEntity<String> updateSpecGroup (SpecGroup group){
        this.specGroupService.updateSpencGroup(group);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping("group/{id}")
    public ResponseEntity<String> deleteSpecGroupById(@PathVariable("id")Long id){
        this.specGroupService.deleteSpecGroupById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping("param")
    public ResponseEntity<String> addSpecParam(SpecParam param){
        this.specGroupService.addSpecParam(param);
        return  new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping("param")
    public ResponseEntity<String> updateSpecParam(SpecParam param){
        this.specGroupService.updateSpecParam(param);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping("param/{id}")
    public ResponseEntity<String> deleteSpecParamById(@PathVariable("id")Long id){
        this.specGroupService.deleteSpecParamById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);

    }
    @GetMapping("{id}")
    public ResponseEntity<List<SpecGroup>> querySpecsByCid(@PathVariable("id") Long cid){
        List<SpecGroup> specGroups = this.specGroupService.querySpecByCid(cid);
        if(specGroups==null || specGroups.size()==0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(specGroups);
    }
}
